package com.defangit.production.orm;

import com.defangit.production.bll.MaterialBillBll;
import com.defangit.production.bll.MaterialsBillBll;
import com.defangit.production.domain.*;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.time.LocalDateTime;

public class MaterialsBillService extends CommonService<MaterialsBill> implements MaterialsBillBll {


    @Override
    public MaterialsBill addBill(MaterialsBill bill, Integer operatorId, Integer processBillId) {
        Operator operator = new OperatorService().get(operatorId).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("用户信息异常"));
        ProductionProcess productionProcess = new ProductionProcessService().get(processBillId).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("委派工单信息异常"));
        bill.setCreateTime(LocalDateTime.now()).setUser(operator);
        add(bill);
        productionProcess.getBills().add(bill);
        getSession().update(productionProcess);
        commitSession();
        return get(bill.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("用料单信息异常"));
    }

    @Override
    public MaterialsBill updateBill(MaterialsBill bill) {
        update(bill);
        commitSession();
        return get(bill.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("用料单信息异常"));
    }

    @Override
    public MaterialsBillFinder findAll() {
        return new MaterialsBillFinderImp(detachedAll());
    }

    public class MaterialsBillFinderImp extends CommonFinder<MaterialsBill> implements MaterialsBillFinder{
        private DetachedCriteria materialsBillCriteria;
        private MaterialsBillFinderImp(DetachedCriteria criteria){
            this.materialsBillCriteria = criteria;
        }

        @Override
        public MaterialsBillFinder materialsId(Integer id) {
            if (id==null) return this;
            this.materialsBillCriteria.add(Restrictions.eq("id",id));
            return this;
        }

        @Override
        public MaterialsBillFinder materialsNo(String no) {
            if (no==null) return this;
            this.materialsBillCriteria.add(Restrictions.eq("no",no));
            return this;
        }

        @Override
        public MaterialsBillFinder operatorId(Integer operatorId) {
            if (operatorId==null) return this;
            this.materialsBillCriteria.add(Restrictions.eq("user",operatorId));
            return this;
        }

        @Override
        public MaterialsBillFinder materialsTime(LocalDateTime min, LocalDateTime max) {
            if (min!=null){
                this.materialsBillCriteria.add(Restrictions.ge("createTime",min));
            }
            if (max!=null){
                this.materialsBillCriteria.add(Restrictions.le("createTime",max));
            }
            return this;
        }

        @Override
        public Criteria getCriteria() {
            return this.materialsBillCriteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return materialsBillCriteria;
        }
    }
}
